package com.luck.emp.payment.logic;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.luck.emp.entity.EmpTPayment;
import com.luck.emp.payment.qry.PaymentQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;

/**
 * 说明 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class PaymentLogic extends BaseLogic implements IPaymentLogic {
	private BaseJdbcDao jdbcDao;

	/**
	 * 查找分页信息
	 */
	public <T> T findPaymentPage(PaymentQry qry, Class<T> cls) {
		Criteria criteria = baseDao.createCriteria(EmpTPayment.class);
		CriteriaUtil.addEq(criteria, "deptId", qry.getDeptId());
		CriteriaUtil.addEq(criteria, "payers", qry.getPayers());
		CriteriaUtil.addEq(criteria, "tel", qry.getTel());
		Order order = Order.desc("id");
		criteria.addOrder(order);

		criteria.createAlias("dept", "d");
		filterData(criteria, qry.getUserInfo(), "d", null);
		if (cls.getName().equals("java.util.List")) {
			return (T) criteria.list();
		} else {
			return (T) baseDao.pagedQuery(criteria, qry.getPageNo(),
					qry.getPageSize());
		}
		// return
		// baseDao.pagedQuery(criteria,qry.getPageNo(),qry.getPageSize());
	}

	/**
	 * 保存
	 **/
	public String savePayment(UserBean userBean, EmpTPayment entity)
			throws Exception {
		StringBuffer bf = new StringBuffer();
		entity.setCreateDate(DateUtil.getDate());
		entity.setCreateUser(userBean.getUser());

		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isNotBlank(bf.toString())) {
			throw new Exception(bf.toString());
		}
		super.save(entity);
		return bf.toString();
	}

	/**
	 * 删除
	 */
	public String deletePayment(UserBean userBean, EmpTPayment entity)
			throws Exception {
		StringBuffer bf = new StringBuffer();

		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isNotBlank(bf.toString())) {
			throw new Exception(bf.toString());
		}
		super.delete(entity);
		return bf.toString();
	}

	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}